package jmercury;

import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.SwingWorker;

public class WaitingWindow extends SwingWorker {

    private boolean Running = true;
    private static boolean wwIsVisible = false;

    private class Window extends JWindow {

        public Window() {
            setAlwaysOnTop(true);
            JLabel jLabel1 = new javax.swing.JLabel();
            setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
            jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jmercury/resources/ktimer.png")));
            jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 64, Short.MAX_VALUE).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addComponent(jLabel1).addGap(0, 0, Short.MAX_VALUE))));
            layout.setVerticalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 64, Short.MAX_VALUE).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addComponent(jLabel1).addGap(0, 0, Short.MAX_VALUE))));
            pack();
            //setLocationRelativeTo(null);
        }
    }

    @Override
    protected Object doInBackground() throws Exception {
        if (wwIsVisible) {
            return true;
        }

        Window w = new Window();
        w.setVisible(true);
        w.paint(w.getGraphics());

        Integer interval = 100;
        int timeout = 150;

        while (Running && timeout > 0) {
            //w.paint(w.getGraphics());
            try {
                Thread.sleep(interval);
            } catch (InterruptedException e) {
            }
            timeout--;
        }

        w.dispose();
        return true;
    }

    public void Stop() {
        Running = false;
        wwIsVisible = false;
    }
}
